from .api import *
from .datastructure import *


def sign(gateway_id, sign_request, api=None):
    api = api or default()
    gateway = api.get_gateway(gateway_id)

    if gateway:
        if hasattr(gateway, 'sign'):
            return gateway.sign(sign_request)
        else:
            raise ValueError('{} not implement {} method'.format(gateway_id, 'sign'))
    else:
        raise ValueError('{} not found.'.format(gateway_id))


def fundin_verify(gateway_id, verify_request, api=None):
    gateway_api = _get_gateway_api(gateway_id)

    if hasattr(gateway_api, 'verify'):
        return gateway_api.verify(verify_request)
    else:
        raise ValueError('{} not implement {} method'.format(gateway_id, 'verify'))


def _get_gateway_api(gateway_id, api=None):
    api = api or default()
    gateway = api.get_gateway(gateway_id)

    if not gateway:
        raise ValueError('{} not found.'.format(gateway_id))
    return gateway
